/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.felinelayer.server.application;

import br.com.felinelayer.common.Message;
import br.com.felinelayer.common.Protocol;
import javax.swing.table.TableModel;

/**
 *
 * @author Home
 */
public class Grid extends Component<Grid> {

    private TableModel tableModel;

    @Override
    public String getComponentClassName() {
        return Protocol.COMPONENT_GRID;
    }

    @Override
    protected void doMessageReceived(Message message) {
        // TODO: how response?
        if (message.isPropertyExists(Protocol.EVENT_NAME)) {
            String eventName = message.getValueByName(Protocol.EVENT_NAME);
            // 
            if (eventName.equals(Protocol.GRID_GET_ROW_COUNT)) {
                message.setValueByName(Protocol.GRID_ROW_COUNT, tableModel.getRowCount());
            }
            //
            if (eventName.equals(Protocol.GRID_GET_COLUMN_COUNT)) {
                message.setValueByName(Protocol.GRID_COLUMN_COUNT, tableModel.getColumnCount());
            }
            // 
            if (eventName.equals(Protocol.GRID_GET_VALUE_CELL)) {
                int row = message.getIntByName(Protocol.GRID_ROW);
                int col = message.getIntByName(Protocol.GRID_COLUMN);
                message.setValueByName(Protocol.GRID_VALUE_CELL, tableModel.getValueAt(row, col).toString());
            }
            // Send response
            sendMessage(message);
        }
    }

    public void setModel(TableModel tableModel) {
        this.tableModel = tableModel;
    }
}
